﻿
using Avalonia;
using Avalonia.Controls;
using DX.ViewModels;
using DX.Views;
using Microsoft.Extensions.Hosting.Internal;
using System.Threading.Tasks;

namespace DX
{
    class DefaultCloseStrategy : ICloseStrategy
    {
        public Task<bool> GetCloseAction(IView view, bool? dialogResult = null)
        {
            if (view is Window form)
            {
                form.Close();
                return Task.FromResult(true);
            }
            if (view is UserControl userControl)
            {
                FindWindow(userControl)?.Close();
                return Task.FromResult(true);

            }

            return Task.FromResult(false);
        }

        private Window FindWindow(StyledElement userControl)
        {
            if (userControl == null) return null;
            if (userControl.Parent is Window window)
            {
                return window;
            }
            return FindWindow(userControl.Parent);
        }
    }
}
